var app = getApp();
import ajaxhelper from '../../utils/ajaxhelper.js';
Page({ 
  data:{
    currentTab:1,  //预设当前项的值
    pageSize: 10,
    pageNo: 1,
    dataList: [],//列表数据
    hasRefresh: false,
    isGetMore: false,
    hasMore: true,
    isShow: false,//是否显示没有列表的提示图
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    var currentTab = option.currentTab;
    if (currentTab){
      this.setData({
        currentTab
      })
    }
    this.getListData(this.data.currentTab); //获取列表数据
  },

  // 列表渲染数据
  getListData:function(id){
    var type;
    if (id == '1') {
      type = "NOT_SEND"
    } else if (id == '2') {
      type = "SEND"
    } else if (id == '3') {
      type = "RECEIVE"
    }
    let param = {
      "type": type,
    }
    var token = wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token;
    ajaxhelper.post(app.globalData.frontJSHost + "present/pkg/queryPage/" + this.data.pageNo + "/" + this.data.pageSize + "?token=" + encodeURIComponent(token), param, this, this.setListData)
  },
  
  setListData(res){
    console.log(res)
    if(res.result == true){
      var Data = res.data;
      var Result = Data.result || '';
      var length = Result.length;
      var pageNo = this.data.pageNo;
      var hasmore = Data.totalCount - pageNo * this.data.pageSize;
      this.setData({
        pageNo: ++pageNo,
        hasRefresh: false,
        isGetMore: false,
        hasMore: hasmore,
      })

      for(var i=0;i <length;i++){
        var remainQty = Result[i].presentPackage.remainQty;
        var totalQty = Result[i].presentPackage.totalQty;
        var price = Result[i].presentPackage.price;
        var prices = price/100;
        Result[i].presentPackage.prices = prices;
        if (this.data.currentTab == '1'){
          Result[i].presentPackage.number = remainQty
        } else if (this.data.currentTab =='2'){
          Result[i].presentPackage.number = totalQty - remainQty  
        } else if (this.data.currentTab == '3'){
          Result[i].presentPackage.number = 1
        }
      }
      if (Data && Result){
        this.setData({
          dataList: this.data.dataList.concat(Result),
        })
      }
    } else {
      this.setData({
        hasRefresh: false,
        isGetMore: false,
      })
      wx.showToast({
        title: !!res.message.global ? res.message.global : "请求出错",
        icon: "loading"
      })
    }
    this.setData({
      isShow: true
    })   
  },

  // 点击标题切换当前页时改变样式
  swichNav:function(e){
    var id = e.currentTarget.dataset.id;
    console.log(id)
    if(this.data.currentTab ==id){
      return false
    }else{
      this.setData({
        currentTab:id,
        pageNo: 1,  
        dataList: [],   
      })
    }
    this.getListData(id)
  },

  //跳转我收到的详情
  todetails:function(e){
    var id = e.currentTarget.dataset.id;
    var type = '';
    var param = {};
    console.log(id)
    if (this.data.currentTab=='1'){
      type = "NOT_SEND"
    } else if (this.data.currentTab == '2'){
      type = "SEND"
    } else if (this.data.currentTab == '3'){
      type = "RECEIVE"
    }   
    var token = wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token;
    ajaxhelper.get(app.globalData.frontJSHost + "present/detail/"+type+"/"+id+"?token="+ encodeURIComponent(token), param, this, this.setAlum)
  },

  setAlum:function(res){
    console.log(res)
    if(res.result){
      var presentPackage = JSON.stringify(res.data.presentPackage);
      var receivers = JSON.stringify(res.data.receivers);
      var presentCard = JSON.stringify(res.data.presentCard);
      console.log(presentPackage)
      console.log(presentCard)
      if (this.data.currentTab == '1') {
        wx.navigateTo({
          url: '../NotSendOutDetail/NotSendOutDetail?&presentPackage=' + presentPackage
        })
      } else if (this.data.currentTab == '2') {
        wx.navigateTo({
          url: '../sendOutDetail/sendOutDetail?&presentPackage=' + presentPackage + '&receivers=' + receivers
        })
      } else if (this.data.currentTab == '3') {
        wx.navigateTo({
          url: '../receivedGifts/receivedGifts?&presentPackage=' + presentPackage + '&presentCard=' + presentCard
        })
      } 
    }else{
      wx.showToast({
        title: !!res.message.global ? res.message.global : "请求出错",
        icon: "loading",
        duration:2000
      })
    } 
  },

  //跳转去观看
  towatch: function (event){
    var id = event.currentTarget.dataset.id;
    var mediaType = event.currentTarget.dataset.mediaType;
    console.log(id)
    console.log(mediaType)
    if (mediaType == 'AUDIO') {
      wx.navigateTo({
        url: '../audioAlbumDetail/audioAlbumDetail?id=' + id
      })
    } else {
      wx.navigateTo({
        url: '../videoAlbumDetail/videoAlbumDetail?id=' + id
      })
    } 
  },

  //下拉刷新
  onPullDownRefresh() {
    this.setData({
      isShow: false
    })
    if (this.data.hasRefresh) return false;
    this.setData({
      dataList: [],
      hasRefresh: true,
      pageNo: 1
    })
    this.getListData(this.data.currentTab)
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1400);
  },
  //上拉加载
  onReachBottom() {
    if (this.data.isGetMore) return false;
    if (this.data.hasRefresh) return false;
    if (this.data.hasMore > 0) {
      this.setData({
        isGetMore: true,
        // tip: '正在加载中...'
      })
      this.getListData(this.data.currentTab);
    } else {
      this.setData({
        isGetMore: false,
        // tip: '没有更多数据了...'
      })
      return false
    }
  },
})